package com.ruoyi.web.controller.lifeCycle;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lifecycle.domain.LifManageList;
import com.ruoyi.lifecycle.service.ILifManageListService;
import com.ruoyi.system.domain.SysDeviceList;
import com.ruoyi.system.domain.SysPurchaseRequisition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 工艺类设备清单
 *
 * @author ruoyi
 * @date 2022-01-04
 */
@Api(tags = "设备管理清单")
@RestController
@RequestMapping("/lifecycle/list")
public class LifManageListController extends BaseController
{
    @Autowired
    private ILifManageListService lifManageListService;

    /**
     * 查询设备生命周期管理清单
     */
    @ApiOperation(value = "设备生命周期管理清单")
    @PreAuthorize("@ss.hasPermi('lifecycle:listmanagement:list')")
    @GetMapping("/list")
    public TableDataInfo list(LifManageList lifManageList)
    {
        startPage();
        List<LifManageList> list = lifManageListService.selectLifManageListList(lifManageList);
        return getDataTable(list);
    }

    /**
     * 获取【合同信息】详细信息
     */
    @ApiOperation(value = "获取【合同信息】详细信息")
    @PreAuthorize("@ss.hasPermi('lifecycle:listmanagement:query')")
    @GetMapping(value = "/get/{deviceId}")
    public AjaxResult getInfoByDeviceId(@PathVariable("deviceId") Long deviceId)
    {
        return AjaxResult.success(lifManageListService.selectSysPurchaseRequisitionByDeviceId(deviceId));
    }


    /**
     * 导出设备清单管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:list:export')")
    @Log(title = "导出设备清单管理列表", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value = "导出设备清单管理列表")
    public AjaxResult export(LifManageList lifManageList)
    {
        List<LifManageList> list = lifManageListService.selectLifManageListList(lifManageList);
        ExcelUtil<LifManageList> util = new ExcelUtil<>(LifManageList.class);
        return util.exportExcel(list, "设备清单管理列表数据");
    }

    @PreAuthorize("@ss.hasPermi('lifecycle:listmanagement:import')")
    @Log(title = "导入设备清单管理列表",businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    @ApiOperation(value = "导入设备清单管理列表")
    public AjaxResult importList(MultipartFile file,Long projectId) throws Exception {

        ExcelUtil<LifManageList> util = new ExcelUtil<>(LifManageList.class);
        List<LifManageList> lifManageLists = util.importExcel(file.getInputStream());
        String operName = getLoginUser().getUsername();
       String message = lifManageListService.importList(lifManageLists,operName,projectId);
       return AjaxResult.success(message);
    }
    /**
     * 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:list:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return AjaxResult.success(lifManageListService.selectLifManageListByDeviceId(deviceId));
    }

    /**
     * 新增工艺设备
     */
    @PreAuthorize("@ss.hasPermi('lifecycle:listmanagement:add')")
    @Log(title = "新增工艺设备", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LifManageList lifManageList)
    {
        return toAjax(lifManageListService.insertLifManageList(lifManageList));
    }

    /**
     * 新增【合同信息】
     */
    @PreAuthorize("@ss.hasPermi('system:list:addContract')")
    @Log(title = "【新增合同信息】", businessType = BusinessType.UPDATE)
    @PutMapping("/addContract")
    public AjaxResult addContract(@RequestBody LifManageList lifManageList)
    {
        return toAjax(lifManageListService.insertSysPurchaseRequisition(lifManageList));
    }

    /**
     * 修改【工艺设备】
     */
    @PreAuthorize("@ss.hasPermi('system:list:update')")
    @Log(title = "【修改工艺设备】", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody LifManageList lifManageList)
    {
        return toAjax(lifManageListService.updateLifManageList(lifManageList));
    }



    /**
     * 批量删除【工艺设备】
     */
    @PreAuthorize("@ss.hasPermi('system:list:remove')")
    @Log(title = "【批量删除工艺设备】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(lifManageListService.deleteLifManageListByDeviceIds(deviceIds));

    }



    /**
     * 工艺设备清单导入模板下载
     * @author ljf
     * @date 2021-12-09 10:22:15
     */
    @ApiOperation(value = "下载工艺设备清单模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<LifManageList> util = new ExcelUtil<LifManageList>(LifManageList.class);
        return util.importTemplateExcel("设备清单数据");
    }
}
